﻿#! python3
# !/usr/bin/python
# -*- coding: UTF-8 -*-
from sys import exit

import os

# 要删除的文件类型列表，
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<
# KEIL file_type = ["o", "crf", "d", "htm"]
# IAR  file_type = ["dep","pbi","o","xcl"]
# KEIL & IAR
file_type = ["o", "crf", "d", "htm", "dep", "pbi", "xcl", "obj"]
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<

dir_amount = 0
dir_deep = 0


# 递归删除子函数
def del_file(dir_in):
    global dir_amount
    global dir_deep
    # global file_type

    # 文件名列表
    fl_list = os.listdir(dir_in)
    dir_amount += 1
    dir_deep += 1
    #print("扫描 -> %02d-%d-%s" % (dir_deep, dir_amount, dir_in))

    # 删除的文件个数
    fl_amount = 0
    for i in range(len(fl_list)):
        namelist = fl_list[i].split(".")
        # 文件的路径 + 名字
        filename = dir_in + "/" + fl_list[i]
        # 如果是文件夹，递归
        if os.path.isdir(filename):
            del_file(filename)
            continue
        # 获取后缀
        suffix = namelist[len(namelist) - 1]
        # 类型检测
        for type_check in file_type:
            if suffix == type_check:
                os.remove(filename)
                fl_amount += 1
                # print("删除的文件：" + filename)
                break
    # 打印删除信息
    if fl_amount > 0:
        print("扫描 -> %02d-%d-%s" % (dir_deep, dir_amount, dir_in))
        print("删除文件数量 ： %d" % fl_amount)

    dir_deep -= 1
    return


# 获取当前路径
path_base = os.getcwd()

# 显示删除信息
del_list = "删除类型::"
for type_x in file_type:
    del_list = del_list + " | ." + type_x
print("目标位置:: " + path_base + "\n")
print(del_list + "\n")

# 操作确认
#del_confirm = input("输入“OK”确认,其他取消: ")
del_confirm = "OK";
del_confirm = del_confirm.upper()
if del_confirm != "OK":
    exit(0)

# 删除
path_src = path_base
del_file(path_src)

input("Enter key to quit!")

exit(0)
